package com.ihr360.cnb.module.insurance.service.impl;

import com.ihr360.authority.embed.base.entity.AuthStaffDataRequest;
import com.ihr360.cnb.module.common.service.Ihr360StaffApiService;
import com.ihr360.cnb.module.common.service.OrganizationService;
import com.ihr360.cnb.module.insurance.service.MonthlyLedgerAggregateService;
import com.ihr360.cnb.module.insurance.service.feign.InsuranceMonthlyLedgerService;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.vo.PageData;
import com.ihr360.excel.ExcelUtil;
import com.ihr360.excel.entity.ExportParams;
import com.ihr360.insurance.entity.request.StaffLedgerRequest;
import com.ihr360.insurance.entity.response.LedgerResponse;
import com.ihr360.insurance.entity.vo.StaffBenefitVo;
import com.ihr360.validation.ValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-05-23 19:47:59.
 */
@Service
public class MonthlyLedgerAggregateServiceImpl implements MonthlyLedgerAggregateService {

    private static final Logger logger = LoggerFactory.getLogger(MonthlyLedgerAggregateServiceImpl.class);

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private Ihr360StaffApiService staffApiService;

    @Autowired
    private InsuranceMonthlyLedgerService monthlyLedgerService;

    @Override
    public PageData<StaffBenefitVo> getCanAddToLedgerStaffs(String companyId, String userId,
                                                            StaffLedgerRequest request,
                                                            String authorityCode) {
        AuthStaffDataRequest staffDataRequest = getStaffDataRequestFromLedgerRequest(request, authorityCode);
        List<String> authStaffIds = staffApiService.getAuthStaffIds(companyId, userId, staffDataRequest);
        if (CollectionUtils.isEmpty(authStaffIds)) {
            return new PageData<>(0, 0, Collections.emptyList());
        }
        request.setStaffIds(authStaffIds);
        return monthlyLedgerService.getAddableList(companyId, userId, request);
    }

    private AuthStaffDataRequest getStaffDataRequestFromLedgerRequest(StaffLedgerRequest request, String authorityCode) {
        AuthStaffDataRequest staffDataRequest = new AuthStaffDataRequest();
        staffDataRequest.setAuthorityCode(authorityCode);
        staffDataRequest.setStaffName(request.getStaffName());
        staffDataRequest.setIdCardNo(request.getIdCardNo());
        staffDataRequest.setDepartIdList(request.getDepartmentIds());
        return staffDataRequest;
    }

    @Override
    public LedgerResponse addAllLedgerStaff(String companyId, String userId, Integer ledgerYear, Integer ledgerMonth, String authorityCode) {
        StaffLedgerRequest req = new StaffLedgerRequest();
        req.setLedgerYear(ledgerYear);
        req.setLedgerMonth(ledgerMonth);
        req.setPageNo(1);
        req.setPageSize(Integer.MAX_VALUE);
        PageData<StaffBenefitVo> canAddToLedgerStaffs = getCanAddToLedgerStaffs(companyId, userId, req, authorityCode);
        return monthlyLedgerService.addLedgerStaff(companyId, userId,
                canAddToLedgerStaffs.getContent(), ledgerYear, ledgerMonth);
    }

    @Override
    public void exportExcel(String companyId, String userId, String requestString, ServletOutputStream outputStream) {
        Map<Long, String> departmentFullNames = organizationService.getDepartmentFullNames(companyId);
        ExportParams exportParams = monthlyLedgerService.getExportData(companyId, userId, requestString, departmentFullNames);
        ExcelUtil.exportExcel(exportParams, outputStream);
        try {
            outputStream.close();
        } catch (IOException e) {
            ValidationUtils.reject("", "数据导出失败,请刷新重试");
            logger.info(e.getMessage());
        }
    }
}
